package com.xzq.erp.controller;


import com.xzq.erp.aspect.ApiOperationLog;
import com.xzq.erp.domain.dto.PageDTO;
import com.xzq.erp.domain.dto.ProductDTO;
import com.xzq.erp.domain.query.ProductPageQuery;
import com.xzq.erp.domain.vo.IdAndNameVO;
import com.xzq.erp.domain.vo.ProductVO;
import com.xzq.erp.service.IProductService;
import com.xzq.erp.utils.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author xzq
 * @since 2025-05-26
 */
@RestController
@RequestMapping("/product")
@RequiredArgsConstructor
@Tag(name = "商品相关接口")
public class ProductController {

    private final IProductService productService;

    @GetMapping("/list")
    @Operation(summary = "商品列表")
    @ApiOperationLog(description = "商品列表")
    public Response<PageDTO<ProductVO>> getProductList(@ParameterObject @Validated ProductPageQuery pageQuery) {
        return Response.success(productService.getProductList(pageQuery));
    }

    @PostMapping
    @Operation(summary = "添加商品")
    @ApiOperationLog(description = "添加商品")
    @PreAuthorize("hasRole('ADMIN')")
    public Response<Void> addProduct(@RequestBody @Validated ProductDTO productDTO) {
        productService.addProduct(productDTO);
        return Response.success();
    }

    @PutMapping
    @Operation(summary = "修改商品")
    @ApiOperationLog(description = "修改商品")
    @PreAuthorize("hasRole('ADMIN')")
    public Response<Void> updateProduct(@RequestBody @Validated ProductDTO productDTO) {
        productService.updateProduct(productDTO);
        return Response.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "商品详情")
    @ApiOperationLog(description = "商品详情")
    public Response<ProductVO> getProduct(@PathVariable Long id) {
        return Response.success(productService.getProductById(id));
    }

    @DeleteMapping
    @Operation(summary = "批量删除商品")
    @ApiOperationLog(description = "批量删除商品")
    @PreAuthorize("hasRole('ADMIN')")
    public Response<Void> deleteProducts(@NotEmpty @RequestBody List<Long> ids) {
        productService.deleteProducts(ids);
        return Response.success();
    }

    @PutMapping("/{id}/{status}")
    @Operation(summary = "商品上下架")
    @ApiOperationLog(description = "商品上下架")
    public Response<Void> updateProductStatus(@PathVariable Long id, @PathVariable Integer status) {
        productService.updateProductStatus(id, status);
        return Response.success();
    }

    /**
     * 根据商品名称模糊查询商品
     */
    @GetMapping
    @Operation(summary = "根据商品名称模糊查询商品")
    @ApiOperationLog(description = "根据商品名称模糊查询商品")
    public Response<List<IdAndNameVO>> getProductsByName(@RequestParam String name) {
        return Response.success(productService.getProductsByName(name));
    }

}
